<template>
  <el-dialog
    title="权限码"
    :visible="show"
    @close="close"
    center>
    <div class="add-pri-code-dialog-box">
      <div class="add-pri-code-tip">设置权限码后，需要填写权限码才可查看博客内容！</div>
      <div class="el-input-box">
        <el-input size="medium" v-model="priCode" placeholder="权限码"/>
      </div>
    </div>
    <span slot="footer" class="add-menu-dialog-footer">
      <el-button type="primary" size="medium" @click="commit">设 置</el-button>
    </span>
  </el-dialog>
</template>

<script>
import Api from "@/utils/api.js";

export default {
  props: ["show", 'blogId'],

  watch: {
    show(newV, oldV) {
      if(newV){
        this.getDetail()
      }
    } 
  },

  data(){
    return{
      priCode: ""
    }
  },

  methods:{
    close(){
      this.$emit('close')
    },

    // 获取权限码
    getDetail(){
      this.$axios.post(Api.BLOG_PRI_CODE_GET, {id: this.blogId})
      .then(res =>{ this.priCode = res})
      .catch(res =>{ this.$message.warning(res.message); })
    },

    // 设置权限码
    commit(){
      this.$axios.post(Api.BLOG_PRI_CODE_SET, {blogId: this.blogId, priCode : this.priCode})
      .then(res =>{ 
        this.$message.success("设置成功!") 
        this.$emit('onChange')
        this.close()
      })
      .catch(res =>{ this.$message.warning(res.message); })
    },
  }
}
</script>

<style lang="less">
@import '@/assets/css/index.less';

// 弹框宽度适配
.el-dialog{
  width: 30vw;
}
@media screen and (max-width: 1023px) {
  .el-dialog{
    width: 300px;
  }
}

.add-pri-code-dialog-box{
  .add-pri-code-tip{
    color: #ff4d4d;
    font-size: 12px;
    font-weight: bold;
    margin-bottom: 15px;
  }

  .el-input-box{
    margin-bottom: 10px;
  }
}

// 添加菜单弹框
.add-menu-dialog-footer{
  
  .el-button{
      width: 200px;
  }
}
</style>
